package com.huawei.app.httpapi;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 公共线程池
 *
 * @since 2021-01-05
 */
public class ThreadPools {

    private static ScheduledExecutorService threadPools = Executors.newScheduledThreadPool(30);

    public static void run(Runnable task) {
        threadPools.submit(task);
    }

    public static void schedule(Runnable task, int delayTime, TimeUnit unit) {
        threadPools.schedule(task, delayTime, unit);
    }

    /**
     * 定时延迟任务
     * @param task
     * @param initialDelayTime
     * @param delayTime
     * @param unit
     */
    public static void scheduleWithFixedDelay(Runnable task, int initialDelayTime, int delayTime, TimeUnit unit) {
        threadPools.scheduleWithFixedDelay(task, initialDelayTime, delayTime, unit);
    }

    /**
     * 定时周期任务
     * @param task
     * @param initialDelayTime
     * @param periodTime
     * @param unit
     */
    public static void scheduleAtFixedRate(Runnable task, int initialDelayTime, int periodTime, TimeUnit unit) {
        threadPools.scheduleAtFixedRate(task, initialDelayTime, periodTime, unit);
    }

    public static void pause(long time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            //
        }
    }
}
